
package msgCenter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.netease.log.NTLog;
import com.netease.util.Util;

import db.XoneDBTables.MsgCenterTable;
import db.manager.ManagerAccount;

public class MsgManager {

    private static int MAX_STROE_COUNTS = 200;
    private static int MAX_DISPLAY_COUNTS = 20;
    /**
     * 获取UI显示所需要的一些数据
     */
    static String[] MsgProject = new String[] {
            MsgCenterTable._ID, // 0
            MsgCenterTable.C_ACCOUNT, // 1
            MsgCenterTable.C_MSGID, // 2
            MsgCenterTable.C_PUSH, // 3
            MsgCenterTable.C_MESSAGE, // 4
            MsgCenterTable.C_TYPE, // 5
            MsgCenterTable.C_READ, // 6
            MsgCenterTable.C_UPDATED, // 7
            MsgCenterTable.C_RESERVE, // 8
    };

    public static int COLUMN_ID = 0;
    public static int COLUMN_MSGID = 2;
    public static int COLUMN_PUSH = 3;
    public static int COLUMN_MESSAGE = 4;
    public static int COLUMN_TYPE = 5;
    public static int COLUMN_READ = 6;
    public static int COLUMN_UPDATED = 7;

    static String[] MsgIdProject = new String[] {
        "MAX(cast (" + MsgCenterTable.C_MSGID + " as int64)) AS maxid"
    };

    static String MSG_SORT = MsgCenterTable.C_UPDATED + " DESC";
    static String MSG_SORT_LIMIT = MsgCenterTable.C_UPDATED + " DESC limit(" + MAX_DISPLAY_COUNTS
            + ")";
    static String MSG_SORT_SINGLE_LIMIT = MsgCenterTable.C_UPDATED + " DESC limit(" + 1 + ")";

    public static Cursor getAllMsg(Context context) {
        if (context == null)
            return null;

        String account = ManagerAccount.getInstance().getCurrentAccountId();
        String where = MsgCenterTable.C_ACCOUNT + " = ? ";
        return context.getContentResolver().query(MsgCenterTable.CONTENT_URI, MsgProject, where,
                new String[] {
                    account
                }, MSG_SORT_LIMIT);
    }

    public static int getMsgCountByType(Context context, int type) {
        int count = 0;
        if (context != null) {
            String account = ManagerAccount.getInstance().getCurrentAccountId();
            String where = MsgCenterTable.C_ACCOUNT + " = ? " + " AND " + MsgCenterTable.C_TYPE
                    + " = ? ";
            Cursor c = context.getContentResolver().query(MsgCenterTable.CONTENT_URI, new String[] {
                MsgCenterTable._ID
            }, where, new String[] {
                    account, String.valueOf(type)
            }, null);
            if (c != null) {
                count = c.getCount();
                c.close();
            }
        }
        return count;
    }

    public static int deleteAllMsgByType(Context context, int type) {
        if (context == null)
            return -1;
        String account = ManagerAccount.getInstance().getCurrentAccountId();
        String where = MsgCenterTable.C_ACCOUNT + " = ? " + " AND " + MsgCenterTable.C_TYPE
                + " = ? ";
        String[] selectionArgs = new String[] {
                account, String.valueOf(type)
        };
        return context.getContentResolver()
                .delete(MsgCenterTable.CONTENT_URI, where, selectionArgs);
    }

    public static Cursor getLatestUnreadMessage(Context context) {
        if (context == null)
            return null;

        String account = ManagerAccount.getInstance().getCurrentAccountId();
        String where = MsgCenterTable.C_ACCOUNT + " = ? " + " AND " + MsgCenterTable.C_READ
                + " = ? ";
        return context.getContentResolver().query(MsgCenterTable.CONTENT_URI, MsgProject, where,
                new String[] {
                        account, "0"
                }, MSG_SORT_SINGLE_LIMIT);
    }

    public static Cursor queryByMsgId(Context context, String msgId) {
        if (context == null)
            return null;
        if (Util.isStringEmpty(msgId))
            return null;

        String account = ManagerAccount.getInstance().getCurrentAccountId();
        String where = MsgCenterTable.C_ACCOUNT + " = ? AND " + MsgCenterTable.C_MSGID + " = ? ";

        return context.getContentResolver().query(MsgCenterTable.CONTENT_URI, new String[] {
            MsgCenterTable._ID
        }, where, new String[] {
                account, msgId
        }, null);
    }

    public static boolean isPushExist(Context context, String msgId) {
        boolean b = false;
        if (Util.isStringEmpty(msgId))
            return true;

        Cursor c = queryByMsgId(context, msgId);
        if (c != null && c.getCount() > 0) {
            b = true;
        }

        if (c != null) {
            c.close();
        }

        return b;
    }

    public static boolean insertPushMsg(Context context, PushMsgParse parse, boolean bRead) {
        if (parse == null)
            return false;

        String account = ManagerAccount.getInstance().getCurrentAccountId();

        ContentValues values = new ContentValues();
        values.put(MsgCenterTable.C_ACCOUNT, account);
        values.put(MsgCenterTable.C_MSGID, parse.msgId);
        values.put(MsgCenterTable.C_PUSH, parse.toString());
        values.put(MsgCenterTable.C_MESSAGE, parse.msg);
        values.put(MsgCenterTable.C_TYPE, parse.msgType);
        values.put(MsgCenterTable.C_UPDATED, parse.rtime);
        if (bRead) {
            values.put(MsgCenterTable.C_READ, "1");
        } else {
            values.put(MsgCenterTable.C_READ, "0");
        }

        context.getContentResolver().insert(MsgCenterTable.CONTENT_URI, values);
        // AppWidgetReceiver.sendDataChangedBroadcast(context); //
        // 发广播通知appwidget更新数据显示

        return true;
    }

    /**
     * push服务器(IM)的最大id
     * 
     * @param context
     * @return
     */
    public static String getMaxMsgId(Context context) {
        // select max(msgid) as maxid from msgcenter
        String account = ManagerAccount.getInstance().getCurrentAccountId();
        String where = MsgCenterTable.C_ACCOUNT + " = ? ";
        String maxid = "";
        Cursor c = context.getContentResolver().query(MsgCenterTable.CONTENT_URI, MsgIdProject,
                where, new String[] {
                    account
                }, null);

        if (c != null && c.moveToFirst()) {
            maxid = c.getString(0);
        }

        if (c != null) {
            c.close();
        }

        NTLog.i("MsgManager", "getMaxMsgId:" + maxid);

        return maxid;
    }
}
